package com.ruyuan.commodity.mq;

import com.ruyuan.commodity.enums.MsgTypeEnums;
import com.ruyuan.commodity.open.api.model.base.message.GoodsChangeMsg;
import com.ruyuan.commodity.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 发送到MQ的消息的工厂
 * @author kimmy
 **/
@Component
public class MqMessageSenderFactory {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(MqMessageSenderFactory.class);

    @Autowired
    private MessageSender messageSender;

    /**
     * 构建并发送消息到MQ
     * @param goodsIds 商品id
     * @param msgTypeEnums 操作类型枚举
     * @param delPromotion 是否删除活动信息
     * @param nameChange 商品名称是否变化
     * @param msg 消息语
     */
    public void createAndSendMsg(Long[] goodsIds, MsgTypeEnums msgTypeEnums,
                                 boolean delPromotion, boolean nameChange,
                                 String msg) {
        GoodsChangeMsg changeMsg = null;
        if (MsgTypeEnums.GOODS_CHANGE_ADD_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_ADD_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_UPDATE_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_UPDATE_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_DELETE_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_DELETE_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_UNDER_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_DELETE_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_IN_RECYCLE_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_IN_RECYCLE_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_REVERT_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_REVERT_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_VERIFY_SUCCESS_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_VERIFY_SUCCESS_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_VERIFY_FAIL_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_VERIFY_FAIL_MSG.getOperationType());
        } else if (MsgTypeEnums.GOODS_CHANGE_PRIORITY_CHANGE_MSG.getOperationType().equals(msgTypeEnums.getOperationType())) {
            changeMsg = new GoodsChangeMsg(goodsIds, MsgTypeEnums.GOODS_CHANGE_PRIORITY_CHANGE_MSG.getOperationType());
        }

        if (!ObjectUtils.isEmpty(changeMsg)) {
            changeMsg.setDelPromotion(delPromotion);
            changeMsg.setNameChange(nameChange);
            if (!StringUtil.isEmpty(msg)) {
                changeMsg.setMessage(msg);
            }
            messageSender.send(new MqMessage(msgTypeEnums.getExchange(),
                    msgTypeEnums.getExchange() + "_ROUTING", changeMsg));
        }
    }

    /**
     * 构建并发送消息到MQ
     * @param delSkuIds 待删除的商品SKU变更id集合
     * @param msgTypeEnums 操作类型枚举
     */
    public void createAndSendGoodsSkuUpdateMsg(List<Long> delSkuIds, MsgTypeEnums msgTypeEnums) {
        if (!CollectionUtils.isEmpty(delSkuIds)) {
            messageSender.send(new MqMessage(msgTypeEnums.getExchange(),
                    msgTypeEnums.getExchange() + "_ROUTING", delSkuIds));
        }
    }

    /**
     * 发送商品分词变化的消息
     * @param word 关键词
     * @param msgTypeEnums 操作类型枚举
     */
    public void createAndSendGoodsWordMsg(String word, MsgTypeEnums msgTypeEnums) {
        if (!StringUtils.isEmpty(word)) {
            messageSender.send(new MqMessage(msgTypeEnums.getExchange(),
                    msgTypeEnums.getExchange() + "_ROUTING", word));
        }
    }

}
